home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Fritz: All Fritz
/
All Fritz.zip
/
All Fritz
/
FILES
/
PROGNG_C
/
TURBOCU1.LZH
/
SOUND.C
< prev
next >
Wrap
Text File
|
1987-09-05
|
4KB
|
153 lines
/*
GENERATING SOUNDS IN TURBO C
Copyright (c), 1987, Telemacus Software Associates
Released to the public domain for private, non commerical
use only.
This file contains three functions that can be employed
to control the speaker:
1. void qsound(unsigned int pitch);
Generates a tone that corresponds to
the "pitch" parameter. The lower
the pitch, the higher the tone.
2. void qnosound();
Stops the speaker.
3. void qdelay_ticks(unsigned int ticks);
Causes a delay corresponding to the
"ticks" parameter. There are approximately
eighteen ticks per second.
QLIB registrants should compile these functions with the
FIXLARGE or REPLACE batch files. Others should use the
command:
tcc -B -ml qsound
QLIB registration involves sending your name, address,
CIS ID, and $35 to:
Lisa T. Vass
5311 Boulevard East # 3
West New York, New Jersey 07093
Registrants receive the lastest QLIB release, complete
documentation, help/include files, and several shell
programs for developing utilities. Updates are provided
free for one year.
*/
void qsound
(
unsigned int pitch
)
{
/*
There are three distinct steps needed
to generate a note:
1. Initialize timer port number 2.
As a trivia aside, timer port
number 0 is used for internal
data transfers and is best
left alone. Timer port number
1 is the clock. Timer port
number 2 is connected (quite
conveniently for this program)
to the speaker. The code to
initialize this port is: */
asm mov al, 0b6h
asm out 043h, al
/*
2. The next step is to load the
the pitch into timer port
number 2. This has to be
done on a byte by byte basis.
The code to do this is: */
asm mov ax, pitch
asm out 042h, al
asm xor ah, al
asm xor al, ah
asm xor ah, al
asm out 042h, al
/*
3. The last step is to turn the
speaker on. This is done
by turning the least significant
bit (which is actually the
tone generation bit) on; and
by turning its neighbor (the
speaker gate bit) on. The
other six bits must remain
undisturbed. The code to
to this is: */
asm in al, 061h
asm or al, 03
asm out 061h, al
/*
At this point, the speaker will be
happily buzzing away. In fact, it will
continue until one of two things happen:
a reboot; or a set of instructions get
executed that turn the speaker off. */
}
void qnosound()
{
/*
Turning the speaker off is fairly
straight-forward. The tone generation
and speaker gate bits must be set to
zero (turned off) while the other
six bits remain undisturbed. The
code for this is: */
asm in al, 061h
asm and al, 0fch
asm out 061h, al
}
void qdelay_ticks
(
unsigned int ticks
)
/*
There are approximately 18 clock ticks per second
recorded in a double word located in the BIOS
communication area. Of particular interest here
is the lower word located at 0:46c. This word
can be queried for an initial value, and further
queried to measure an elapsed time. The code
to perform this function is given below. Note
that a delay in seconds can be performed simply
by factoring the "tick" value.
qdelay_ticks(5 * 18); for example, will
generate a delay of approximately five
seconds.
*/
{
unsigned far *timerlow = {(unsigned far *) 0x46c};
unsigned start_time;
unsigned current_time = 0;
start_time = *timerlow;
do
current_time = *timerlow;
while (current_time - start_time < ticks);
}
. ... ...-.... 1200 N81N